package com.hengtiansoft.lfy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.entity.NewsEntity;
import com.hengtiansoft.lfy.widget.LoadingView;

/**
 * Created by daiye on 2016/10/26 0026.
 */

public class NewsDetailActivity extends BaseActivity {
    public static final String EXTRA_NEWS = "NEWS";
    private ImageView iv_back = null;
    private TextView mNewsTitleTv = null;
    private LinearLayout mNewsContentLl = null;
    private TextView mNewsComeTv = null;
    private TextView mNewsCreatetimeTv = null;
    private TextView mNewsCreatorTv = null;
    private int mNewsId = -1;
    private NewsEntity.DataEntity mNewsDetailEntity = null;
    private LoadingView mLoading = null;

    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private void initData(){
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if(bundle!=null){
                mNewsDetailEntity = (NewsEntity.DataEntity) bundle.getSerializable(EXTRA_NEWS);
            }
        }
    }

    private void initView() {
        setContentView(R.layout.activity_newsdetail);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            public final void onClick(View v) {
                finish();
            }
        });
        mLoading = (LoadingView) findViewById(R.id.layout_loading);
        mNewsTitleTv = (TextView) findViewById(R.id.tv_newtitle);
        mNewsComeTv = (TextView) findViewById(R.id.tv_news_come);
        mNewsCreatetimeTv = (TextView) findViewById(R.id.tv_create_time);
        mNewsCreatorTv = (TextView) findViewById(R.id.tv_creator);

        if(mNewsDetailEntity!=null){
            String pic = mNewsDetailEntity.getPic();
            String create_time = mNewsDetailEntity.getCreate_time();
            String creator = mNewsDetailEntity.getCreator();
            String source = mNewsDetailEntity.getSource();
            String title = mNewsDetailEntity.getTitle();
            String sday = mNewsDetailEntity.getS_day();
            mNewsId = mNewsDetailEntity.getId();
            if(!TextUtils.isEmpty(title)) {
                mNewsTitleTv.setText(title);
            }else
                mNewsTitleTv.setText("");
            if(!TextUtils.isEmpty(source)) {
                mNewsComeTv.setText(source);
            }else
                mNewsComeTv.setText("");
            if(!TextUtils.isEmpty(sday)) {
                mNewsCreatetimeTv.setText(sday);
            }else
                mNewsCreatetimeTv.setText("");
            if(!TextUtils.isEmpty(creator)) {
                mNewsCreatorTv.setText(creator);
            }else
                mNewsCreatorTv.setText("");
        }

        //mNewsContentLl = (LinearLayout) findViewById(R.id.layout_newscontent);

        // 打开页面时， 自适应屏幕
        mWebView = (WebView) findViewById(R.id.news_content_web);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);  //支持缩放
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        webSettings.setJavaScriptEnabled(true); // 启用支持javascript
        webSettings.setLoadsImagesAutomatically(true);  //支持自动加载图片
        webSettings.setBlockNetworkImage(false);
        mWebView.setBackgroundColor(0); // 背景透明

        refreshNewsDetailView();
    }

    private void refreshNewsDetailView(){
        mLoading.setVisibility(View.GONE);
        String data = "<html> \n" +
                "\n" +
                "  \n" +
                "\n" +
                "<head>  \n" +
                "\n" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\"> \n" +
                "\n" +
                "<title>血</title> \n" +
                "\n" +
                "</head> \n" +
                "\n" +
                "  \n" +
                "\n" +
                "<body background=\"images/e44e2397c394d44154fb967f[1].jpg\" text=red> \n" +
                "\n" +
                "<font size=5>  \n" +
                "\n" +
                " \n" +
                "\n" +
                " \n" +
                "\n" +
                "<p align=center>&nbsp;&nbsp;血</p> \n" +
                "<p align=center>&nbsp;&nbsp;浓</p> \n" +
                "<p align=center>&nbsp;&nbsp;于</p> \n" +
                "<p align=center>&nbsp;&nbsp;水</p> \n" +
                "\n" +
                "</font> \n" +
                "</body> \n" +
                "\n" +
                "</html> ";
        //mWebView.loadData(URLEncoder.encode(data, "utf-8"), "text/html",  "utf-8");
        //mWebView.loadDataWithBaseURL(null,data, "text/html",  "utf-8", null);

        String url = Constants.SERVER_HOST + "images/news/html/" + mNewsId + ".html";
        mWebView.loadUrl(url);
        //mWebView.loadUrl("http://www.sina.com");
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }
}
